﻿Imports System.Runtime.CompilerServices

Public Module StdControls

	<Extension()> _
	Public Sub LoadItems(ByVal c As ComboBox, ByVal Source As List(Of SourceItem))
		Dim Rs As New Recordset("kod(s),ad(s)")
		For Each Item In Source
			Rs.AddNew("kod,ad", Item.Kod, Item.Ad)
		Next
		c.AutoCompleteSource = AutoCompleteSource.ListItems
		c.AutoCompleteMode = AutoCompleteMode.SuggestAppend
		c.DisplayMember = "ad"
		c.ValueMember = "kod"
		c.DataSource = Rs
	End Sub

	<Extension()> _
	Public Sub LoadItems(ByVal c As DataGridViewComboBoxColumn, ByVal Source As List(Of SourceItem))
		Dim Rs As New Recordset("kod(s),ad(s)")
		For Each Item In Source
			Rs.AddNew("kod,ad", Item.Kod, Item.Ad)
		Next
		c.AutoComplete = True
		'c.AutoCompleteSource = AutoCompleteSource.ListItems
		'c.AutoCompleteMode = AutoCompleteMode.SuggestAppend
		c.DisplayMember = "ad"
		c.ValueMember = "kod"
		c.DataSource = Rs
	End Sub

	<Extension()> _
	Public Sub LoadItems(ByVal c As ComboBox, ByVal Items As String)
		Dim Rs As New Recordset("kod(s),ad(s)")
		For Each Item In Items.Split(",")
			Dim Data = Item.Split(":")
			Rs.AddNew("kod,ad", Data(0), Data(1))
		Next
		c.AutoCompleteSource = AutoCompleteSource.ListItems
		c.AutoCompleteMode = AutoCompleteMode.SuggestAppend
		c.DisplayMember = "ad"
		c.ValueMember = "kod"
		c.DataSource = Rs
	End Sub

	<Extension()> _
	Public Sub LoadItems(ByVal c As ComboBox, ByVal Rs As Recordset)
		c.AutoCompleteSource = AutoCompleteSource.ListItems
		c.AutoCompleteMode = AutoCompleteMode.SuggestAppend
		c.DisplayMember = Rs.Columns(1).ColumnName
		c.ValueMember = Rs.Columns(0).ColumnName
		c.DataSource = Rs
	End Sub

	<Extension()> _
	Function Value(ByVal Combo As ComboBox)
		If Combo.SelectedValue Is Nothing Then Return Nothing
		Return Combo.SelectedValue
	End Function

	<Extension()> _
	Sub SetValue(ByVal Combo As ComboBox, ByVal Value As Object)
		If Value Is Nothing Then Value = ""
		If IsDBNull(Value) Then Value = ""
		Combo.SelectedValue = Value
	End Sub

	<Extension()> _
	Sub Clear(ByVal Combo As ComboBox)
		Combo.SelectedValue = DBNull.Value
	End Sub

	<Extension()> _
	Function Empty(ByVal Combo As ComboBox) As Boolean
		If Combo.SelectedValue Is Nothing Then Return True
		If IsDBNull(Combo.SelectedValue) Then Return True
		Return False
	End Function

	Public Class SourceItem

		Public Kod As String
		Public Ad As String

		Sub New(Kod As String, Ad As String)
			Me.Kod = Kod
			Me.Ad = Ad
		End Sub

	End Class

	<Extension()>
	Sub Highlight(row As DataGridViewRow)
		row.DataGridView.ClearSelection()
		row.Selected = True
		row.DataGridView.FirstDisplayedScrollingRowIndex = row.Index
		row.DataGridView.CurrentCell = (From c As DataGridViewCell In row.Cells Where c.Visible).FirstOrDefault
	End Sub
End Module

